package com.wrupple.muba.catalogs.client.cms.service.impl;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.TakesValue;
import com.google.inject.Provider;
import com.wrupple.muba.catalogs.client.activity.CatalogActivity;
import com.wrupple.muba.catalogs.client.activity.process.CatalogEditingProcess;
import com.wrupple.muba.catalogs.client.activity.process.CatalogSelectionProcess;
import com.wrupple.muba.catalogs.client.activity.process.task.CatalogInteractionState.Mode;
import com.wrupple.muba.catalogs.client.cms.service.BrowserWidgetAssembler;
import com.wrupple.muba.catalogs.client.cms.service.CatalogFormAssembler;
import com.wrupple.muba.catalogs.client.cms.service.CatalogManager;

public class SimpleCatalogManager<V extends JavaScriptObject> implements
		CatalogManager<V> {


	protected String managedCatalog;
	private Provider<? extends CatalogSelectionProcess<V>> selectionProvider;
	private Provider<? extends CatalogEditingProcess<V>> editionProvider;
	private Provider<? extends BrowserWidgetAssembler> browserAssemblerProvider;
	private Provider<? extends CatalogFormAssembler> editorAssemblerProvider;


	public SimpleCatalogManager(
			String managedCatalog,
			Provider<? extends CatalogSelectionProcess<V>> selectionProvider,
			Provider<? extends CatalogEditingProcess<V>> editionProvider,
			Provider<? extends BrowserWidgetAssembler> browserAssemblerProvider,
			Provider<? extends CatalogFormAssembler> readerAssemblerProvider) {
		super();
		this.managedCatalog = managedCatalog;
		this.selectionProvider = selectionProvider;
		this.editionProvider = editionProvider;
		this.browserAssemblerProvider = browserAssemblerProvider;
		this.editorAssemblerProvider = readerAssemblerProvider;
	}



	@Override
	public String getManagedCatalog() {
		return managedCatalog;
	}

	@Override
	public CatalogSelectionProcess<V> getSelectionProcess(boolean allowMultiple) {
		CatalogSelectionProcess<V> regreso = this.selectionProvider.get();
		regreso.allowMultipleSelection(allowMultiple);
		return regreso;
	}

	@Override
	public CatalogEditingProcess<V> getEditingProcess(Mode mode, EventBus bus) {
		CatalogEditingProcess<V> regreso = editionProvider.get();
		regreso.getEditor().getEditor().initialize(managedCatalog, mode, bus);
		return regreso;
	}
	
	@Override
	public TakesValue<V> getUserTransaction(
			String transactionType, JavaScriptObject properties,
			EventBus bus) {
		TakesValue regreso=null;
		if(CatalogActivity.SUMMARY_ACTION.equals(transactionType)){
			regreso = browserAssemblerProvider.get().assembleDataWidget(properties,bus);
		}
		if(CatalogActivity.READ_ACTION.equals(transactionType)){
			regreso = editorAssemblerProvider.get().assembleReadingWidget(properties,bus,Mode.READ);
		}
		if(CatalogActivity.UPDATE_ACTION.equals(transactionType)){
			regreso = editorAssemblerProvider.get().assembleReadingWidget(properties,bus,Mode.UPDATE);
		}
		if(regreso==null){
			throw new IllegalArgumentException("Transaction "+transactionType+" is not supported for this Content Manager ( catalog="+getManagedCatalog()+" )");
		}
		
		return regreso;
	}


}
